package com.yaorange.guild.user.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.guild.user.entity.BsMember;
import com.yaorange.guild.user.service.BsMemberService;
import com.yaorange.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bsMember")
public class BsMemberController {

    @Autowired
    private BsMemberService bsMemberService;

    /**
     * 查询所有记录，并按id降序排列
     *
     * @param bsMember 查询条件
     * @return 响应结果
     */
    @GetMapping
    public Result selectAll(BsMember bsMember) {
        List<BsMember> list = bsMemberService.selectAll(bsMember);
        return Result.success(list);
    }

    /**
     * 分页查询记录
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @param bsMember 查询条件
     * @return 响应结果
     */
    @GetMapping("/page")
    public Result selectPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            BsMember bsMember) {
        PageInfo<BsMember> pageInfo = bsMemberService.selectPage(pageNum, pageSize, bsMember);
        return Result.success(pageInfo);
    }

    /**
     * 根据主键查询记录
     *
     * @param id 主键
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id) {
        BsMember bsMember = bsMemberService.selectById(id);
        if (bsMember == null) {
            return Result.fail("记录不存在");
        }
        return Result.success( bsMember);
    }

    /**
     * 插入记录
     *
     * @param bsMember 记录对象
     * @return 响应结果
     */
    @PostMapping
    public Result insert(@RequestBody BsMember bsMember) {
        int rows = bsMemberService.insert(bsMember);
        if (rows > 0) {
            return Result.success("插入成功");
        }
        return Result.fail("插入失败");
    }

    /**
     * 更新记录
     *
     * @param bsMember 记录对象
     * @return 响应结果
     */
    @PutMapping
    public Result updateById(@RequestBody BsMember bsMember) {
        int rows = bsMemberService.updateById(bsMember);
        if (rows > 0) {
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    /**
     * 根据主键删除记录
     *
     * @param id 主键
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id) {
        int rows = bsMemberService.deleteById(id);
        if (rows > 0) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }
}
